package client.comm.baoding.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import client.comm.baoding.R;
import client.comm.commlib.utils.StringUtil;
import client.comm.commlib.utils.ToastUtil;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @author:
 * @date: 2016-08-01 14:24
 * @GitHub: https://github.com/meikoz
 */
public class DialogManager {

    private static SweetAlertDialog mDialog;

    public static void showWarningDialog(Context context, String title, String content, SweetAlertDialog.OnSweetClickListener listener) {
        if (mDialog != null) {
            mDialog = null;
        }
        mDialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
                .setTitleText(title)
                .setContentText(content)
                .setConfirmText("确定")
                .setCancelText("取消")
                .setConfirmClickListener(listener);
        mDialog.show();
    }

    public static void showErrorDialog(Context context, String title, String content, SweetAlertDialog.OnSweetClickListener listener) {
        if (mDialog != null) {
            mDialog = null;
        }
        mDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE)
                .setConfirmText("确定")
                .setTitleText(title)
                .setContentText(content)
                .setConfirmClickListener(listener);
        mDialog.show();
    }



    public static void showProgressDialog(Context context, String message, int progress) {
        if (mDialog != null) {
            mDialog = null;
        }
        mDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        mDialog.getProgressHelper().setBarColor(context.getResources().getColor(R.color.colorPrimary));
        mDialog.setTitleText(message);
        mDialog.setCancelable(true);
        mDialog.getProgressHelper().setProgress(progress);
        mDialog.show();
    }


    public static final String alert = "提示";
    public static String cancel = "取消";
    public static final String confirm = "确定";
    /**
     *
     * 纯文字提示框
     * @param context
     * @param hint
     * @param title
     * @param obj
     * @param what
     * @param arg1
     * @param handler
     * @param arg2
     * @param cancelable
     * @param isFinish
     * @param activity
     */
    public static void dlgOneBtn(final Context context, final String hint, final String title, final Object obj,
                                 final int what,
                                 final int arg1, final Handler handler, final int arg2, final boolean cancelable, final boolean isFinish, final  Activity activity,String confirm,String cancel) {

        final P2pAlertDialog dialog = new P2pAlertDialog(context);
        dialog.builder().setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setCancelBtn(false);
        dialog.setMsg(hint);
        dialog.setPositiveButton(confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isFinish){
                    activity.finish();
                }else{
                    Message msg = handler.obtainMessage(
                            what, arg1, 1, obj);
                    msg.sendToTarget();
                }
                dialog.dismiss();
            }
        }).setNegativeButton(cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isFinish){
                    activity.finish();
                }else{
                    Message msg = handler.obtainMessage(
                            what, arg1, 0, obj);
                    msg.sendToTarget();
                    dialog.dismiss();
                }
                dialog.dismiss();
            }
        }).show();

    }
    /**
     *
     * 纯文字提示框
     * @param context
     * @param hint
     * @param title
     * @param obj
     * @param what
     * @param arg1
     * @param handler
     * @param arg2
     * @param cancelable
     * @param isFinish
     * @param activity
     */
    public static void dlgTwoBtn(final Context context, final String hint, final String title, final Object obj,
                                 final int what,
                                 final int arg1, final Handler handler, final int arg2, final boolean cancelable, final boolean isFinish, final  Activity activity,String confirm,String cancel) {

        final P2pAlertDialog dialog = new P2pAlertDialog(context);
        dialog.builder().setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setMsg(hint);
        dialog.setPositiveButton(confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = handler.obtainMessage(
                        what, arg1, 1, obj);
                msg.sendToTarget();

                dialog.dismiss();
            }
        }).setNegativeButton(cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isFinish){
                    activity.finish();
                }else{
                    Message msg = handler.obtainMessage(
                            what, arg1, 0, obj);
                    msg.sendToTarget();
                    dialog.dismiss();
                }
                dialog.dismiss();
            }
        }).show();

    }
    /**
     *
     * @param context
     * @param hint
     * @param title
     * @param obj
     * @param what
     * @param arg1
     * @param handler
     * @param cancelable
     * @param isFinish
     * @param activity
     */
    public static void dlgTwoBtnCancelInput(final Context context, final String hint, final String title, final Object obj,
                                 final int what,
                                 final int arg1, final Handler handler, final boolean cancelable, final boolean isFinish, final  Activity activity) {

        final P2pAlertDialog dialog = new P2pAlertDialog(context);
        dialog.builder().setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setMsg(hint);
        dialog.setPositiveButton(confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = handler.obtainMessage(
                        what, arg1, 1, obj);
                msg.sendToTarget();

                dialog.dismiss();
            }
        }).setNegativeButton(cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Message msg = handler.obtainMessage(
                        what, arg1, 0, obj);
                msg.sendToTarget();
                dialog.dismiss();
            }
        }).show();

    }
    /**
     *
     * @param context
     * @param hint
     * @param title
     * @param obj
     * @param what
     * @param arg1
     * @param handler
     * @param arg2
     * @param cancelable
     * @param isFinish
     * @param activity
     */
    public static void inputButton(final Context context, final String hint, final String title , final Object obj,
                                 final int what,
                                 final int arg1, final Handler handler, final int arg2, final boolean cancelable, final boolean isFinish, final  Activity activity) {

        final P2pAlertDialog dialog = new P2pAlertDialog(context);
        dialog.builder().setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setEdt(hint);
        dialog.setInput(true);
        dialog.setPositiveButton(confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StringUtil.isNullOrEmpty(dialog.getEdt())){
                    ToastUtil.show(hint,context);
                    return;
                }
                Message msg = handler.obtainMessage(
                        what, arg1, arg2, dialog.getEdt().toString());
                msg.sendToTarget();
                dialog.dismiss();
            }
        }).setNegativeButton(cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isFinish){
                    activity.finish();
                }
                dialog.dismiss();
            }
        }).show();

    }
    /**
     *
     * @param context
     * @param hint
     * @param title
     * @param obj
     * @param what
     * @param arg1
     * @param handler
     * @param cancelable
     * @param isFinish
     * @param activity
     * 左右两边都掉接口
     */
    public static void inputButtonAdd(final Context context, final String hint, final String title , final String confirm,String cancel,final Object obj,
                                   final int what,
                                   final int arg1, final Handler handler, final boolean cancelable, final boolean isFinish, final  Activity activity) {

        final P2pAlertDialog dialog = new P2pAlertDialog(context);
        dialog.builder().setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setEdt(hint);
        dialog.setInput(true);
        dialog.setPositiveButton(confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StringUtil.isNullOrEmpty(dialog.getEdt())){
                    ToastUtil.show(hint,context);
                    return;
                }
                Message msg = handler.obtainMessage(
                        what, arg1, 1, dialog.getEdt().toString());
                msg.sendToTarget();
                dialog.dismiss();
            }
        }).setNegativeButton(cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Message msg = handler.obtainMessage(
                        what, arg1, 0, dialog.getEdt().toString());
                msg.sendToTarget();
                dialog.dismiss();
                dialog.dismiss();
            }
        }).show();

    }
    /**
     *
     * @param context
     * @param hint
     * @param title
     * @param obj
     * @param what
     * @param arg1
     * @param handler
     * @param arg2
     * @param cancelable
     * @param isFinish
     * @param activity
     */
    public static void inputButtonHasHint(final Context context, final String title,final String hint, final String textHint, final String textContent
            ,final String leftBtn,final String rightBtn, final Object obj,
                                   final int what,
                                   final int arg1, final Handler handler, final int arg2, final boolean cancelable, final boolean isFinish, final  Activity activity) {

        final P2pAlertDialog dialog = new P2pAlertDialog(context);
        dialog.builder().setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setMsg(hint);
        dialog.setEdt(textHint);
        dialog.setEdtContent(textContent);
        dialog.setInputHint(true);
        dialog.setPositiveButton(rightBtn, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StringUtil.isNullOrEmpty(dialog.getEdt())){
                    ToastUtil.show(hint,context);
                    return;
                }
                Message msg = handler.obtainMessage(
                        what, arg1, arg2, dialog.getEdt().toString());
                msg.sendToTarget();
                dialog.dismiss();
            }
        }).setNegativeButton(leftBtn, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isFinish){
                    activity.finish();
                }
                dialog.dismiss();
            }
        }).show();

    }
}
